import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberAccountService extends BaseService<any> {
  private readonly logger = new Logger(MemberAccountService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberAccountService_api::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $where[ 'member_id' ] = $this->member_id;         $field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, me...
where[ 'member_id' ] = this.member_id;
        const field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';
        const search_model = this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'member_id', 'account_type', 'from_type', 'create_time', 'account_data_gt', 'account_data_lt', 'keyword' ), where).field(field).order('id desc').append([ 'from_type_name', 'account_type_name' )];
        return this.pageQuery(search_model);
    }

    /**
     * 会员积分流水列表
     * @param where
     * @return */
    async getPointPage(where = [))
    {
        const type_where = [];
        switch (where[ 'amount_type' )) {
            case 'income':
                const type_where = [
                    [ 'account_data', '>', 0 ],
                ];
                break;
            case 'disburse':
                const type_where = [
                    [ 'account_data', '<', 0 ],
                ];
                break;
            default:
                break;
  }

  /**
   * getPointPage
   * 对应 PHP: MemberAccountService_api::getPointPage()
   * 逻辑类型: undefined - undefined
   */
  async getPointPage(where: any[]) {
    // 基于PHP真实逻辑: getPointPage
    // PHP原文: $type_where = [];         switch ($where[ 'amount_type' ]) {             case 'income':                 $type_where = [                     [ 'account...
const type_where = [];
        switch (where[ 'amount_type' )) {
            case 'income':
                const type_where = [
                    [ 'account_data', '>', 0 ],
                ];
                break;
            case 'disburse':
                const type_where = [
                    [ 'account_data', '<', 0 ],
                ];
                break;
            default:
                break;
  }

  /**
   * getBalancePage
   * 对应 PHP: MemberAccountService_api::getBalancePage()
   * 逻辑类型: undefined - undefined
   */
  async getBalancePage(data: any[]) {
    // 基于PHP真实逻辑: getBalancePage
    // PHP原文: switch ($data[ 'trade_type' ]) {             case 'income':                 $type_where = [                     [ 'account_type', 'in', [ MemberAccoun...
switch (data[ 'trade_type' )) {
            case 'income':
                const type_where = [
                    [ 'account_type', 'in', [ MemberAccountTypeDict.BALANCE, MemberAccountTypeDict.MONEY ] ],
                    [ 'account_data', '>', 0 ],
                    [ 'from_type', '<>', 'cash_out' ]
                ];
                break;
            case 'disburse':
                const type_where = [
                    [ 'account_type', 'in', [ MemberAccountTypeDict.BALANCE, MemberAccountTypeDict.MONEY ] ],
                    [ 'account_data', '<', 0 ],
                    [ 'from_type', '<>', 'cash_out' ]
                ];
                break;
            case 'cash_out':
                const type_where = [
                    [ 'account_type', '=', MemberAccountTypeDict.MONEY ],
                    [ 'from_type', '=', 'cash_out' ]
                ];
                break;
            default:
                const type_where = [
                    [ 'account_type', 'in', [ MemberAccountTypeDict.BALANCE, MemberAccountTypeDict.MONEY ] ],
                ];
                break;
  }

  /**
   * getInfo
   * 对应 PHP: MemberAccountService_api::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';         return $this->model->where([ [ 'id', ...
const field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ) )).field(field).append([ 'from_type_name', 'account_type_name' )).findOrEmpty().toArray(];
    }

    /**
     * 会员账户统计数量
     * @param where
     * @return int
     * @throws DbBusinessException
     */
    async getCount(where = [))
    {
        where[ 'member_id' ] = this.member_id;

        return this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'member_id', 'account_type', 'from_type', 'create_time' ), where).count(];
    }

    /**
     * 会员积分统计数量
     * @return int[)
     */
    async getPointCount()
    {
        const data = [
            point: 0,
            point_get: 0,
            use: 0,//使用
        ];
        const info = ( this.memberService ).where([ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ) )).field('point,point_get').findOrEmpty().toArray(];
        data[ 'point' ] = info[ 'point' ] || 0;
        data[ 'point_get' ] = info[ 'point_get' ] || 0;
        data[ 'use' ] = abs(this.model.where([
            [ 'site_id', '=', this.site_id ],
            [ 'member_id', '=', this.member_id ],
            [ 'account_type', '=', MemberAccountTypeDict.POINT ],
            [ 'account_data', '<', 0 )
        )).sum('account_data')];

        return data;
    }

    /**
     * 按年月分组数据（例如账单）
     * @param arr_data 分组的数组
     * @param string time_field 时间分组字段
     * @return */
    function monthlyGrouping(arr_data, time_field = 'create_time')
    {
        if (!time_field)) {
            return arr_data;
  }

  /**
   * getCount
   * 对应 PHP: MemberAccountService_api::getCount()
   * 逻辑类型: undefined - undefined
   */
  async getCount(where: any[]) {
    // 基于PHP真实逻辑: getCount
    // PHP原文: $where[ 'member_id' ] = $this->member_id;          return $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'member_id', 'acco...
where[ 'member_id' ] = this.member_id;

        return this.model.where([ [ 'site_id', '=', this.site_id ) )).withSearch([ 'member_id', 'account_type', 'from_type', 'create_time' ), where).count(];
    }

    /**
     * 会员积分统计数量
     * @return int[)
     */
    async getPointCount()
    {
        const data = [
            point: 0,
            point_get: 0,
            use: 0,//使用
        ];
        const info = ( this.memberService ).where([ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ) )).field('point,point_get').findOrEmpty().toArray(];
        data[ 'point' ] = info[ 'point' ] || 0;
        data[ 'point_get' ] = info[ 'point_get' ] || 0;
        data[ 'use' ] = abs(this.model.where([
            [ 'site_id', '=', this.site_id ],
            [ 'member_id', '=', this.member_id ],
            [ 'account_type', '=', MemberAccountTypeDict.POINT ],
            [ 'account_data', '<', 0 )
        )).sum('account_data')];

        return data;
    }

    /**
     * 按年月分组数据（例如账单）
     * @param arr_data 分组的数组
     * @param string time_field 时间分组字段
     * @return */
    function monthlyGrouping(arr_data, time_field = 'create_time')
    {
        if (!time_field)) {
            return arr_data;
  }

  /**
   * getPointCount
   * 对应 PHP: MemberAccountService_api::getPointCount()
   * 逻辑类型: undefined - undefined
   */
  async getPointCount() {
    // 基于PHP真实逻辑: getPointCount
    // PHP原文: $data = [             'point' => 0,             'point_get' => 0,             'use' => 0,//使用         ];         $info = ( new Member() )->where([ [ '...
const data = [
            point: 0,
            point_get: 0,
            use: 0,//使用
        ];
        const info = ( this.memberService ).where([ [ 'site_id', '=', this.site_id ], [ 'member_id', '=', this.member_id ) )).field('point,point_get').findOrEmpty().toArray(];
        data[ 'point' ] = info[ 'point' ] || 0;
        data[ 'point_get' ] = info[ 'point_get' ] || 0;
        data[ 'use' ] = abs(this.model.where([
            [ 'site_id', '=', this.site_id ],
            [ 'member_id', '=', this.member_id ],
            [ 'account_type', '=', MemberAccountTypeDict.POINT ],
            [ 'account_data', '<', 0 )
        )).sum('account_data')];

        return data;
    }

    /**
     * 按年月分组数据（例如账单）
     * @param arr_data 分组的数组
     * @param string time_field 时间分组字段
     * @return */
    function monthlyGrouping(arr_data, time_field = 'create_time')
    {
        if (!time_field)) {
            return arr_data;
  }
}
